package com.bestom.bluetoothlock.bluetooth;


import com.bestom.bluetoothlock.util.LogMsg;

public class StringOper {
    public static int fourByteToInt(byte b, byte b2, byte b3, byte b4) {
        return ((b & 255) << 24) | ((b2 & 255) << 16) | ((b3 & 255) << 8) | (b4 & 255);
    }

    public static int twoByteToInt(byte b, byte b2) {
        return ((b & 255) << 8) | (b2 & 255);
    }

    public static byte hexStringtoByte(String str) {
        if (str == null || str.equals("") || str.length() > 2) {
            return 0;
        }
        char[] charArray = str.toUpperCase().toCharArray();
        return (byte) (charToByte(charArray[1]) | (charToByte(charArray[0]) << 4));
    }

    public static String toHexString(int i) {
        String hexString = Integer.toHexString(i);
        if (hexString.length() >= 2) {
            return hexString.length() > 2 ? hexString.substring(hexString.length() - 2) : hexString;
        }
        return "0" + hexString;
    }

    public static byte[] hexStringToBytes(String str) {
        if (str == null || str.equals("")) {
            return null;
        }
        String upperCase = str.toUpperCase();
        int length = upperCase.length() / 2;
        char[] charArray = upperCase.toCharArray();
        byte[] bArr = new byte[length];
        for (int i = 0; i < length; i++) {
            int i2 = i * 2;
            bArr[i] = (byte) (charToByte(charArray[i2 + 1]) | (charToByte(charArray[i2]) << 4));
        }
        return bArr;
    }

    public static String bytesToHexString(byte[] bArr) {
        String str = "";
        for (byte hexString : bArr) {
            String hexString2 = Integer.toHexString(hexString);
            if (hexString2.length() < 2) {
                hexString2 = "0" + hexString2;
            } else if (hexString2.length() > 2) {
                hexString2 = hexString2.substring(hexString2.length() - 2);
            }
            str = str + hexString2;
        }
        return str;
    }

    public static int getDataLen(byte[] bArr) {
        if (bArr.length >= 11) {
            return twoByteToInt(bArr[3], bArr[2]);
        }
        LogMsg.Mess("无效数据:" + bytesToHexString(bArr));
        return -1;
    }

    public static int getDataType(byte[] bArr) {
        if (bArr.length >= 4) {
            return twoByteToInt(bArr[1], bArr[0]);
        }
        LogMsg.Mess("无效数据:" + bytesToHexString(bArr));
        return -1;
    }

    public static boolean isVerify(byte[] bArr) {
        int i = 0;
        for (int i2 = 0; i2 < bArr.length - 1; i2++) {
            i += bArr[i2];
        }
        return ((short) (i & 255)) == ((short) (bArr[bArr.length - 1] & 255));
    }

    public static byte[] combinationByte(byte[] bArr, byte[] bArr2) {
        int length = bArr.length + bArr2.length;
        byte[] bArr3 = new byte[length];
        for (int i = 0; i < length; i++) {
            if (i < bArr.length) {
                bArr3[i] = bArr[i];
            } else {
                bArr3[i] = bArr2[i - bArr.length];
            }
        }
        return bArr3;
    }

    public static String getHerSn(byte[] bArr) {
        byte[] bArr2 = new byte[15];
        for (int i = 0; i < bArr2.length; i++) {
            bArr2[i] = bArr[i + 28];
        }
        return new String(bArr2);
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
}
